﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace Dal
{
    public class DalNavigation
    {
        SqlConnection conn = crecteSqlConnection.sqlconn();
        SqlCommand cmd = null;
        string sql = "";
        public bool Update_Information(ModelPublic m)
        {
            //一级二级导航修改
            SqlConnection conn = crecteSqlConnection.sqlconn();
            conn.Open();
            try
            {
                if (m.SqlId=="2")
                    sql = "update SubNavigation set Name=" + m.Name + ",EnglishName=" + m.EnglishName + ",Sort=" + m.Sort + ",Show=" + m.Show + ",BelongTo=" + m.BelongTo + ",WebAdress=" + m.Web + " where Id=" + m.Id + "";
                if (m.SqlId=="1")
                    sql = "update Navigation set Name=" + m.Name + ",Sort=" + m.Sort + ",Show=" + m.Show + ",WebAdress=" + m.Web + " where Id=" + m.Id + "";

                cmd = new SqlCommand(sql, conn);
                if (cmd.ExecuteNonQuery() > 0)
                    return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return false;
        }
        
        public DataTable Nav_Name_All()
        {
            //后台管理一级导航显示
            //NavContent
            SqlConnection conn = crecteSqlConnection.sqlconn();
            conn.Open();
            DataTable dt = null;
            try
            {
                sql = "select Id,Name,Sort,Show,WebAdress from Navigation where Id between 1 and 11";

                cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dt = ds.Tables[0];
                return dt;
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return dt;
        }

        public DataTable SubNavContent(ModelPublic m)
        {
            //二级菜单内容
            SqlConnection conn = crecteSqlConnection.sqlconn();
            conn.Open();
            DataTable dt = null;
            try
            {
                string sql = "";

                sql = "select * from SubNavigation where BelongTo='" + m.NavId + "' and Deleted='false' order by Sort asc";
                cmd = new SqlCommand(sql, conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds, "table");
                dt = ds.Tables[0];
                return dt;
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return dt;
        }

        public bool Data_Delete(ModelPublic m)
        {
            //二级导航删除
            SqlConnection conn = crecteSqlConnection.sqlconn();
            conn.Open();
            try
            {
                sql = "update SubNavigation set Deleted='True' where Id='" + m.Id + "'";

                cmd = new SqlCommand(sql, conn);
                if (cmd.ExecuteNonQuery() > 0)
                    return true;
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return false;
        }

        public bool SubNav_Add(ModelPublic m)
        {
            //二级导航添加
            SqlConnection conn = crecteSqlConnection.sqlconn();
            conn.Open();
            try
            {
                sql = "insert into SubNavigation(Name,EnglishName,Sort,BelongTo,Show,WebAdress)values(" + m.Name + "," + m.EnglishName + "," + m.Sort + "," + m.BelongTo + "," + m.Show + "," + m.Web + ")";

                cmd = new SqlCommand(sql, conn);
                if (cmd.ExecuteNonQuery() > 0)
                    return true;
            }
            catch (Exception)
            {

            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return false;
        }
    }
}
